package com.xinke.module.system.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.user.*;
import com.xinke.module.system.model.entity.user.RoleDO;
import com.xinke.module.system.service.user.RoleService;

@Tag(name = "管理后台 - 角色信息")
@RestController
@RequestMapping("/system/role")
@Validated
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/create")
    @Operation(summary = "创建角色信息")
    @PreAuthorize("@ss.hasPermission('system:role:create')")
    public Result<Long> createRole(@Valid @RequestBody RoleSaveReqVO createReqVO) {
        return success(roleService.createRole(createReqVO,null));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色信息")
    @PreAuthorize("@ss.hasPermission('system:role:update')")
    public Result<Boolean> updateRole(@Valid @RequestBody RoleSaveReqVO updateReqVO) {
        roleService.updateRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete/{roleId}")
    @Operation(summary = "删除角色信息")
    @Parameter(name = "roleId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:role:delete')")
    public Result<Boolean> deleteRole(@PathVariable("roleId") Long roleId) {
        roleService.deleteRole(roleId);
        return success(true);
    }

    @GetMapping("/get/{roleId}")
    @Operation(summary = "获得角色信息")
    @Parameter(name = "roleId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public Result<RoleRespVO> getRole(@PathVariable("roleId") Long roleId) {
        RoleDO role = roleService.getRole(roleId);
        return success(BeanUtils.toBean(role, RoleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色信息分页")
    @PreAuthorize("@ss.hasPermission('system:role:query')")
    public Result<PageResult<RoleRespVO>> getRolePage(@Valid RolePageReqVO pageReqVO) {
        PageResult<RoleDO> pageResult = roleService.getRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RoleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出角色信息 Excel")
    @PreAuthorize("@ss.hasPermission('system:role:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRoleExcel(@Valid RolePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RoleDO> list = roleService.getRolePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "角色信息.xls", "数据", RoleRespVO.class,
                        BeanUtils.toBean(list, RoleRespVO.class));
    }

}